import { _decorator, Component, Size, UITransform, v3, view } from 'cc';
import DataManager from '../Runtime/DataManager';
import { G } from '../Common';
const { ccclass, property } = _decorator;

@ccclass('BattleCamera')
export class BattleCamera extends Component {

    viewSize: Size

    onLoad() {
        this.viewSize = view.getVisibleSize()
    }
    update() {

        const player = DataManager.Ins.actorMap.get(DataManager.Ins.myId)
        if (player) {
            const uiTransform = player.getComponent(UITransform)
            let newVec3 = uiTransform.convertToWorldSpaceAR(
                v3(
                    -view.getVisibleSize().width / 2,
                    -view.getVisibleSize().height / 2,
                    0
                )
            )

            const w = this.viewSize.width / 2 - 50
            const h = this.viewSize.height / 2 - 50
            if (G.StageSize.w > this.viewSize.width) {
                if (newVec3.x > G.StageSize.w / 2 - w) {
                    newVec3.x = G.StageSize.w / 2 - w
                } else if (newVec3.x < -G.StageSize.w / 2 + w) {
                    newVec3.x = -G.StageSize.w / 2 + w
                }
            } else {
                newVec3.x = 0
            }

            if (G.StageSize.h > this.viewSize.height) {
                if (newVec3.y > G.StageSize.h / 2 - h) {
                    newVec3.y = G.StageSize.h / 2 - h
                } else if (newVec3.y < -G.StageSize.h / 2 + h) {
                    newVec3.y = -G.StageSize.h / 2 + h
                }
            } else {
                newVec3.y = 0
            }

            this.node.setPosition(newVec3.x, newVec3.y)
        }
    }

}


